package com.lanyou.esb.service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.lanyou.esb.repository.ServiceAlarmStatisticsRepository;
import com.lanyou.esb.vo.IfAlarmStatisticsVo;
import com.lanyou.esb.vo.IfAlarmStatisticsVo.Entry;
import com.lanyou.esb.web.param.IfAlarmStatisticsParam;

/**
 * 服务响应时间服务
 * 
 * @author Davey.wu
 */
@Service
public class ServiceAlarmStatisticsService {
	@Autowired
	private ServiceAlarmStatisticsRepository serviceAlarmStatisticsRepository;

	/**
	 * 获取服务告警统计信息
	 * 
	 * @param param
	 * @param pageable
	 * @return
	 */
	public IfAlarmStatisticsVo findServiceAlarmStatisticsInfo(
			IfAlarmStatisticsParam param, Pageable pageable) {
		IfAlarmStatisticsVo vo = new IfAlarmStatisticsVo();
		// 查看总记录数
		List<Object> totals = serviceAlarmStatisticsRepository
				.findServiceAlarmStatisticsTotal(param.getFieldId(),
						param.getSystemId(), param.getIfName(),
						param.getAlarmType(), param.getStartDate(),
						param.getEndDate());
		BigInteger total = null;
		if (totals != null && totals.size() > 0) {
			total = (BigInteger) totals.get(0);
		}
		// 无记录，返回空集
		if (total == null || total.longValue() <= 0) {
			vo.setTotal(0);
			vo.setEntrys(new ArrayList<>(0));
			return vo;
		}
		// 获取数据
		List<Object[]> objects = serviceAlarmStatisticsRepository
				.findServiceAlarmStatisticsInfo(param.getFieldId(),
						param.getSystemId(), param.getIfName(),
						param.getAlarmType(), param.getStartDate(),
						param.getEndDate(),
						pageable.getPageNumber() * pageable.getPageSize(),
						pageable.getPageSize());
		// 设置总数
		vo.setTotal(total.longValue());
		List<Entry> entrys = new ArrayList<>(objects.size());
		vo.setEntrys(entrys);
		Entry entry = null;
		// 转换数据
		for (Object[] object : objects) {
			entry = new Entry();
			entry.setFieldName(object[0] != null ? (String) object[0] : "");
			entry.setSystemName(object[1] != null ? (String) object[1] : "");
			entry.setIfName(object[2] != null ? (String) object[2] : "");
			entry.setIfStatus(object[3] != null ? (String) object[3] : "");
			entry.setAlarmType(object[4] != null ? (String) object[4] : "");
			entry.setTimes(object[5] != null ? ((BigDecimal) object[5])
					.longValue() : 0l);
			entrys.add(entry);
		}
		return vo;
	}

	/**
	 * 获取服务告警统计信息
	 * 
	 * @param param
	 * @return
	 */
	public IfAlarmStatisticsVo findServiceAlarmStatisticsInfo(
			IfAlarmStatisticsParam param) {
		IfAlarmStatisticsVo vo = new IfAlarmStatisticsVo();
		// 获取数据
		List<Object[]> objects = serviceAlarmStatisticsRepository
				.findServiceAlarmStatisticsInfo(param.getFieldId(),
						param.getSystemId(), param.getIfName(),
						param.getAlarmType(), param.getStartDate(),
						param.getEndDate());
		// 无记录，返回空集
		if (objects == null || objects.isEmpty()) {
			vo.setTotal(0);
			vo.setEntrys(new ArrayList<>(0));
		}
		List<Entry> entrys = new ArrayList<>(objects.size());
		vo.setEntrys(entrys);
		Entry entry = null;
		// 转换数据
		for (Object[] object : objects) {
			entry = new Entry();
			entry.setFieldName(object[0] != null ? (String) object[0] : "");
			entry.setSystemName(object[1] != null ? (String) object[1] : "");
			entry.setIfName(object[2] != null ? (String) object[2] : "");
			entry.setIfStatus(object[3] != null ? (String) object[3] : "");
			entry.setAlarmType(object[4] != null ? (String) object[4] : "");
			entry.setTimes(object[5] != null ? ((BigDecimal) object[5])
					.longValue() : 0l);
			entrys.add(entry);
		}
		return vo;
	}

}
